package com.bx.implatform.controller;

import com.bx.implatform.annotation.RepeatSubmit;
import com.bx.implatform.dto.*;
import com.bx.implatform.entity.GroupMember;
import com.bx.implatform.result.Result;
import com.bx.implatform.result.ResultUtils;
import com.bx.implatform.service.GroupService;
import com.bx.implatform.vo.GroupMemberVO;
import com.bx.implatform.vo.GroupVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "群聊")
@RestController
@RequestMapping("/imApi/group")
@RequiredArgsConstructor
public class GroupController {

    private final GroupService groupService;

    @RepeatSubmit
    @Operation(summary = "创建群聊", description = "创建群聊")
    @PostMapping("/create")
    public Result<GroupVO> createGroup(@Valid @RequestBody GroupVO vo) {
        return ResultUtils.success(groupService.createGroup(vo));
    }

    @RepeatSubmit
    @Operation(summary = "修改群聊信息", description = "修改群聊信息")
    @PutMapping("/modify")
    public Result<GroupVO> modifyGroup(@Valid @RequestBody GroupVO vo) {
        return ResultUtils.success(groupService.modifyGroup(vo));
    }

    @RepeatSubmit
    @Operation(summary = "解散群聊", description = "解散群聊")
    @DeleteMapping("/delete/{groupId}")
    public Result deleteGroup(@NotNull(message = "群聊id不能为空") @PathVariable Long groupId) {
        groupService.deleteGroup(groupId);
        return ResultUtils.success();
    }


    @Operation(summary = "查询群聊", description = "查询单个群聊信息")
    @GetMapping("/find/{groupId}")
    public Result<GroupVO> findGroup(@NotNull(message = "群聊id不能为空") @PathVariable Long groupId) {
        return ResultUtils.success(groupService.findById(groupId));
    }

    @Operation(summary = "查询群聊列表", description = "查询群聊列表")
    @GetMapping("/list")
    public Result<List<GroupVO>> findGroups() {
        return ResultUtils.success(groupService.findGroups());
    }

    @RepeatSubmit
    @Operation(summary = "邀请进群", description = "邀请好友进群")
    @PostMapping("/invite")
    public Result invite(@Valid @RequestBody GroupInviteDTO dto) {
        groupService.invite(dto);
        return ResultUtils.success();
    }

    // @RepeatSubmit
    @Operation(summary = "自动入群", description = "通过事件自动入群")
    @PostMapping("/automaticGroup")
    public Result automaticGroup(@Valid @RequestBody GroupAutomaticDTO dto) {
        groupService.automaticGroup(dto);
        return ResultUtils.success();
    }

    @Operation(summary = "查询群聊成员", description = "查询群聊成员")
    @GetMapping("/members/{groupId}")
    public Result<List<GroupMemberVO>> findGroupMembers(
        @NotNull(message = "群聊id不能为空") @PathVariable Long groupId) {
        return ResultUtils.success(groupService.findGroupMembers(groupId));
    }

    @RepeatSubmit
    @Operation(summary = "将成员移出群聊", description = "将成员移出群聊")
    @DeleteMapping("/members/remove")
    public Result removeMembers(@Valid @RequestBody GroupMemberRemoveDTO dto) {
        groupService.removeGroupMembers(dto);
        return ResultUtils.success();
    }


    @RepeatSubmit
    @Operation(summary = "消息免打扰", description = "消息免打扰")
    @PostMapping("/members/mute")
    public Result muteGroupMembers(@Valid @RequestBody GroupMemberMuteDTO dto) {
        return ResultUtils.success(groupService.muteGroupMembers(dto));
    }

    @RepeatSubmit
    @Operation(summary = "退出群聊", description = "退出群聊")
    @DeleteMapping("/quit/{groupId}")
    public Result quitGroup(@NotNull(message = "群聊id不能为空") @PathVariable Long groupId) {
        groupService.quitGroup(groupId);
        return ResultUtils.success();
    }

    @RepeatSubmit
    @Operation(summary = "踢出群聊(已废弃)", description = "将用户踢出群聊")
    @DeleteMapping("/kick/{groupId}")
    public Result kickGroup(@NotNull(message = "群聊id不能为空") @PathVariable Long groupId,
        @NotNull(message = "用户id不能为空") @RequestParam String userId) {
        groupService.kickGroup(groupId, userId);
        return ResultUtils.success();
    }

    @RepeatSubmit
    @Operation(summary = "绑定群聊和会议室关系", description = "绑定群聊和会议室关系")
    @PostMapping("/roomAdd")
    public Result groupRoomAdd(@Valid @RequestBody GroupRoomDTO dto) {
        return ResultUtils.success(groupService.groupRoomAdd(dto));
    }

    @Operation(summary = "根据会议室ID查询群聊ID", description = "根据会议室ID查询群聊ID")
    @GetMapping("/roomQuery/{roomId}")
    public Result<GroupRoomDTO> findGroupIdByRoomId(
            @NotNull(message = "会议室id不能为空") @PathVariable String roomId) {
        return ResultUtils.success(groupService.findGroupIdByRoomId(roomId));
    }

    @Operation(summary = "根据群ID查询会议室ID", description = "根据群ID查询会议室ID")
    @GetMapping("/roomQueryByGroupId/{groupId}")
    public Result<GroupRoomDTO> findRoomIdByGroupId(
            @NotNull(message = "群id不能为空") @PathVariable Long groupId,
            @Parameter(description = "会议室状态 0生效 1失效", examples = {
                    @ExampleObject(name = "生效", value = "0"),
                    @ExampleObject(name = "失效", value = "1")
            })
            @RequestParam(required = false) Integer roomStatus) {
        return ResultUtils.success(groupService.findRoomIdByGroupId(groupId,roomStatus));
    }

    @RepeatSubmit
    @Operation(summary = "删除群聊和会议室的绑定关系", description = "删除群聊和会议室的绑定关系")
    @DeleteMapping("/roomDel/{roomId}")
    public Result groupRoomDel(@NotNull(message = "会议室id不能为空") @PathVariable String roomId) {
        return ResultUtils.success(groupService.groupRoomDel(roomId));
    }

    @GetMapping("/closeRoomsByGroupId")
    @Operation(summary = "根据群聊ID查询失效会议室信息", description = "根据群聊ID查询失效会议室信息")
    public Result<List<GroupRoomDTO>> closeRoomsByGroupId(@NotNull(message = "群聊id不能为空") @RequestParam Long groupId,
                                                      @NotNull(message = "页码不能为空") @RequestParam Long page,
                                                      @NotNull(message = "size不能为空") @RequestParam Long size,
                                                      @NotNull(message = "会议室类型不能为空")@RequestParam String roomType) {
        return ResultUtils.success(groupService.closeRoomsByGroupId(groupId, page, size, roomType));
    }
}

